package cn.cxq;

import java.util.ArrayList;

/**
 * Oracle工具类
 */
public class OracleSQLUtil {

    /**
     * 产生获取表对象
     *
     * @param sql Oracle建表sql语句
     * @return 表对象
     */
    public static Table getTable(String sql) {

        Table table = new Table();

        String[] split = sql.split("-- "); // 将输入分割

//        for (String s : split) {
//            System.out.println(s);
//        }

        // 获取表名称
        String[] split1 = split[1].split(" ");
        table.setTableName(split1[3].replaceFirst("(.*)\\.", "").replaceFirst("\\(", "").toLowerCase());

        String line = split[1].replaceFirst("(.*)[a-zA-Z_]\\( ", "").replaceFirst("\\)tablespace(.*)", "");
//        System.out.println(line);

        String[] columns = line.split(", "); // 获取所有的字段

//        for (String column : columns) {
//            System.out.println(column);
//        }


        ArrayList<Table.Column> cList = table.getColumns();

        for (String column : columns) {
            String[] s = column.split(" ");
            Table.Column c = new Table.Column();
            c.columnName = s[0];
            c.columnType = getColumnType(s[1]);

            cList.add(c);
        }

        // 获取表注释
        if (split[2].matches("Add comments to the table(.*)")) {
            String temp = split[2].replaceFirst("Add comments to the table comment on table (.*) is ", "");
            String comment = temp.substring(0, temp.length() - 1);
            table.setTableComment(comment);
        }

        // 获取所有字段注释
        if (split[2].matches("Add comments to the columns(.*)")) {
            getColumnComment(table, split[2]);
        } else if (split[3].matches("Add comments to the columns(.*)")) {
            getColumnComment(table, split[3]);
        }

        return table;
    }

    /**
     * 获取单个字段注释
     *
     * @param table 表对象
     * @param s     注释行
     */
    private static void getColumnComment(Table table, String s) {
        s = s.replace("Add comments to the columns ", "");
        String[] comments = s.split("';");

        ArrayList<Table.Column> columns = table.getColumns();

        for (String comment : comments) {
            for (Table.Column column : columns) {
                if (comment.contains("." + column.columnName)) {
                    column.columnComment = comment.replaceFirst("comment on column (.*) is '", "");
                }
            }
        }
    }

    /**
     * 获取hive字段类型
     *
     * @param columnType oracle原来的字段类型
     * @return hive字段类型
     */
    private static String getColumnType(String columnType) {
        if (columnType.matches("date|DATE|timestamp(.*)|TIMESTAMP(.*)|TIMESTAMP")) {
            return "timestamp";
        }

        if (columnType.matches("varchar(.*)|VARCHAR(.*)")) {
            return "string";
        }

        if (columnType.matches("number|integer|NUMBER|INTEGER|number\\(\\d+\\)|integer\\(\\d+\\)|NUMBER\\(\\d+\\)|INTEGER\\(\\d+\\)")) {
            return "bigint";
        }

        if (columnType.matches("number\\(\\d+,\\d+\\)|NUMBER\\(\\d+,\\d+\\)|integer\\(\\d+,\\d+\\)|INTEGER\\(\\d+,\\d+\\)")) {
            return columnType.replaceFirst("(.*)\\(", "decimal(");
        }

        return columnType;
    }

}
